/* 
 * Persistence4J - Simple library for data persistence using java
 * Copyright (c) 2010, Avdhesh yadav.
 * http://www.avdheshyadav.com
 * Contact: avdhesh.yadav@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

package com.avdheshyadav.p4j.jdbc.dbms;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.avdheshyadav.p4j.jdbc.PersistenceConfig;


/**
 * @author Avdhesh Yadav   
 */
class DbmsManager 
{
	private static Logger logger = Logger.getLogger(DbmsManager.class.getName());
	
	private static DbmsManager _Instance;
	/** */
	private PersistenceConfig mConfig;
	/** */
	private static String DATABASES_KEY = "persistence.databases";
	/** */
	Map<String, Dbms> m_DbmsMap;
	
	private String[] m_DbmsNames;

	/**
	 * 
	 * @param props Properties
	 * @return DbmsManager
	 */
	public static DbmsManager getDbmsManager(PersistenceConfig props)
	{
		if(_Instance == null)
		{
		 _Instance = new DbmsManager(props);
		}
		return _Instance;
	}
	
	
	/**
	 * 
	 */
	private DbmsManager(PersistenceConfig props)
	{
		
		mConfig = props;
		initilize();
	}
	
	
	/**
	 * In this method we initilize the dbms
	 */
	private void initilize()
	{
		logger.info("inside initialize method");
		
		m_DbmsMap = new HashMap<String, Dbms>();
		
		String dbmss = mConfig.getDbmss();
		
		m_DbmsNames = dbmss.split(",");
		
		for(String dbmsName : m_DbmsNames )
		{
			logger.info("DBMS Name :"+dbmsName);
			Dbms dbms = DbmsFactory.getDBMS(dbmsName, mConfig);
			m_DbmsMap.put(dbmsName, dbms);
		}
	}
	
	
	/**
	 * 
	 * @param dsName String
	 * 
	 * @return Dbms
	 */
	public Dbms getDBMS(String dbmsName)
	{
		logger.info("dbmsName:"+dbmsName);
		
		Dbms dbms = m_DbmsMap.get(dbmsName);
		System.out.println("dbms:"+dbms + " dbmsName:"+dbmsName);
		return dbms;
	}
}